package no.uib.hplims.views;

import java.util.ArrayList;
import java.util.Iterator;

import org.vaadin.appfoundation.view.AbstractView;
import no.uib.hplims.Controller;
import no.uib.hplims.MyVaadinApplication;
import no.uib.hplims.views.auth.Logout;
import no.uib.hplims.views.experiments.ExperimentsTab;
import no.uib.hplims.views.help.Help;
import no.uib.hplims.views.home.Home;
import no.uib.hplims.views.orders.OrderTab;
import no.uib.hplims.views.search.AllPeptidesView;
import no.uib.hplims.views.settings.SettingsTab;

import com.vaadin.data.Property;
import com.vaadin.ui.Component;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Tree;
import com.vaadin.ui.VerticalLayout;

public class MenuTabSheet extends AbstractView<VerticalLayout> {

	private static final long serialVersionUID = 2625342161669216333L;

	private Controller controller = null;

	private VerticalLayout layout = null;
	private TabSheet mainTabs = new TabSheet();
	private VerticalLayout detailslayout = new VerticalLayout();
	private Tree menu = null;

	private ArrayList<Component> views = new ArrayList<Component>();

	

	public MenuTabSheet() {
		super(new VerticalLayout());

		// Create the root layout (VerticalLayout is actually the default).
		VerticalLayout root = new VerticalLayout();
		root.setSizeFull();

		// Add the components

		// Horizontal layout with selection tree on the left and
		// a details panel on the right.
		HorizontalLayout horlayout = new HorizontalLayout();
		horlayout.setSizeFull();
		horlayout.setSpacing(true);
		root.addComponent(horlayout);
		root.setExpandRatio(horlayout, 1);

		// Layout for the menu area. Wrap the menu in a Panel to allow
		// scrollbar.
		Panel menuContainer = new Panel("HP LIMS");
		menuContainer.addStyleName("menucontainer");
		menuContainer.addStyleName("light"); // No border
		menuContainer.setWidth("-1px"); // Undefined width
		menuContainer.setHeight("100%");
		menuContainer.getContent().setWidth("-1px"); // Undefined width
		horlayout.addComponent(menuContainer);

		// A menu tree, fill it later.
		menu = new Tree();
		menu.setSizeUndefined();
		menuContainer.addComponent(menu);

		// A panel for the main view area on the right side

		// Have a vertical layout in the Details panel.
		detailslayout.setSizeFull();
		horlayout.addComponent(detailslayout);

		// Let the details panel take as much space as possible and
		// have the selection tree to be as small as possible
		horlayout.setExpandRatio(detailslayout, 1);
		horlayout.setExpandRatio(menuContainer, 0);

		// ////////////////////////////////////////////////////
		// Put in the application data and handle the UI logic

		Controller controller = MyVaadinApplication.getInstance().getController();
		controller.setCurrentView(new Home());
		
		views.add(controller.getCurrentView());
		OrderTab orderTab = new OrderTab();
		MyVaadinApplication.getInstance().getController().setOrderTab(orderTab);
		views.add(orderTab);
		ExperimentsTab expTab = new ExperimentsTab();
		MyVaadinApplication.getInstance().getController().setExpTab(expTab);
		views.add(expTab);
		views.add(new AllPeptidesView());
		views.add(new SettingsTab());
		views.add(new Help());
		views.add(new Logout());

		for (Component view : views) {
			menu.addItem(view);
		}

		detailslayout.addComponent(controller.getCurrentView());

		menu.addListener(new Property.ValueChangeListener() {
			private static final long serialVersionUID = 3632470400712122093L;

			public void valueChange(Property.ValueChangeEvent event) {
				if (event.getProperty() != null
						&& event.getProperty().getValue() != null) {

					activate((Component) event.getProperty().getValue());
				}
			}
		});
		menu.setImmediate(true);

		layout = getContent();
		layout.setSizeFull();
		layout.addComponent(root);

		mainTabs.setSizeFull();

		// layout.addComponent(mainTabs);
	}

	public void activate(Component view) {
		if (!(view instanceof Component)) {
			throw new IllegalArgumentException("View must be a component");
		}

		detailslayout.replaceComponent((Component) controller.getCurrentView(),
				(Component) view);
		controller.setCurrentView((AbstractParentTab) view);
		controller.getCurrentView().activated();
	}

	public void activated(Object... params) {
		if (controller == null) {
			MyVaadinApplication app = (MyVaadinApplication) this
					.getApplication();
			controller = app.getController();
		}
		
		controller.setMainMenu(this);
	}

	public void deactivated(Object... params) {
		// TODO Auto-generated method stub

	}

	public VerticalLayout getDetailslayout() {
		return detailslayout;
	}
}
